<?php 
	class com_content extends component
	{
		public $option;
		public $view;
		public $layout;
		public $id;
		public $page;

		public function get_content()
		{
			$option = 'com_content';
			$view = $_GET['view'];
			$layout = $_GET['layout'];
			$id = $_GET['id'];
			$page = $_GET['page'];
			$posts_per_page = 5;
			switch ($view) {
				case 'article':
					$this->get_post($id);
					break;
				case 'category':
					if ($layout == 'blog') { //Blog style
						$cat = $this->get_cat($id);
						if ($cat !== null) {
							$cats = $this->get_cats_list($cat[cat_path]);
							$this->create_breadcrumbs($cats);
						}
						if (!isset($page) || ($page == ''))
							$page = 1;
						$posts = $this->get_posts($cat,$page,$posts_per_page);
						$this->show_blog_style($posts);
						$this->pagination($cat,$page,$posts_per_page);
					}
					else { //List style

					}
					break;
				default:
					$this->show_featured_posts();
					get_module('recentPosts','3');
					break;
			}
		}
		/* Get content from Database */
		//Get the post
		private function get_post($p_id)
		{
			//Cookie not exist or expired
			if (CHECKCOOKIE == false) {
				$this->update_post_hit($p_id);
			}
			//Get the post
			$query = "SELECT p.title,p.content,p.intro,c.cat_path,p.post_meta,p.post_robots,p.meta_title,p.hit,p.meta_description,p.created_by,p.created_on,p.allow_comment FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id WHERE p.state=1 AND p.id=$p_id";
			$result = mysql_query($query) or die(mysql_error());
			$num_row = mysql_num_rows($result);
			if ($num_row > 0) {
				$post = mysql_fetch_array($result);
				$date = date_create($post[created_on]);
				$cat_path = $post[cat_path];
				$cats = $this->get_cats_list($cat_path);
				$this->create_breadcrumbs($cats);
?>
				<div class="post-outer">
					<div class="meta-data">
						<div class="date">
							<strong class="date_month"><?php echo date_format($date,'M'); ?></strong>
							<strong class="date_day"><?php echo date_format($date,'d'); ?></strong>
							<strong class="date_year"><?php echo date_format($date,'Y'); ?></strong>
						</div><!-- End .date-->
						<h2 class="post-title"><?php echo $post[title]; ?></h2>
						<span class="author-vcard" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
							Post by:<span class="author-name" itemprop="name"><?php echo $post[created_by]; ?></span><br/>
							View:<span class="author-name" itemprop="name"><?php echo $post[hit]; ?></span>
						</span>
					</div><!-- End .meta-data-->
					<div class="post-content">
						<?php echo $post[intro].$post[content]; ?>
					</div><!-- End .post-content-->
<?php				
					if (intval($post[allow_comment]) == 1){
						//Get comment component
						require_once('component/comment/index.php');
						$con = new com_comment;
						$con->set_post_id($p_id);
						$con->get_content();
					}
?>
				</div><!-- End .post-outer-->
<?php
			}
			else {
				echo '<div class="post-outer">There\'s no post!</div>';
			}
		}
		//Create breadcrumbs
		private function create_breadcrumbs($cats)
		{
?>
			<div class="breadcrumbs" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb">
				<a class="first" href="<?php echo BASEURL;?>" itemprop="url"><span itemprop="title">Home</span></a>
<?php
			foreach ($cats as $cat) {
				$url = BASEURL.'/index.php?option=com_content&view=category&layout=blog&id='.$cat[id];
?>
				<a href="<?php echo $url; ?>" itemprop="url"><span itemprop="title"><?php echo $cat[category_name];?></span></a>
<?php
			}
?>
			</div><!-- End .breadcrumbs-->
<?php
		}
		//Get categories list
		private function get_cats_list($cat_path)
		{
			$list = str_replace('>', ',', $cat_path);
			$len = strlen($list);
			$list = substr($list,0,$len-1);
			$list = '('.$list.')';
			$query = "SELECT c.id,c.category_name FROM categories as c WHERE c.id IN $list ORDER BY c.display_order";
			$result = mysql_query($query) or die(mysql_error());
			$rows = array();
			while ($row = mysql_fetch_array($result))
				$rows[] = $row;
			return $rows;
		}
		//Get all posts in category
		private function get_posts($cat,$page,$posts_per_page)
		{
			$rows = array();
			$from = ($page - 1)*$posts_per_page;
			if ($cat !== null) {
				$query = "SELECT p.id as post_id,p.title,p.intro,p.hit,c.id as cat_id,p.post_meta,p.post_robots,p.meta_title,p.meta_description,p.created_by,p.created_on FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id WHERE p.state=1 AND c.state=1 AND c.cat_path LIKE '".$cat[cat_path]."%' ORDER BY p.created_on DESC LIMIT $from,$posts_per_page";
				$result = mysql_query($query) or die(mysql_error());
				$num_row = mysql_num_rows($result);
				if ($num_row > 0) {
					while ($row = mysql_fetch_array($result))
						$rows[] = $row;
					return $rows;
				}
			}
			return null;
		}
		//Get category by id
		private function get_cat($cat_id)
		{
			$query = "SELECT c.category_name,c.cat_path FROM categories as c WHERE c.id=$cat_id";
			$result = mysql_query($query) or die(mysql_error());
			$num_row = mysql_num_rows($result);
			$row = array();
			if ($num_row > 0) {
				$row = mysql_fetch_array($result);
				return $row;
			}
			else 
				return null;
			
		}
		//Layout = blog
		private function show_blog_style($posts)
		{
			if ($posts !== null) {
				foreach ($posts as $post) {
					$date = date_create($post[created_on]);
					$url = BASEURL.'/index.php?option=com_content&view=article&id='.$post[post_id];
?>
					<div class="post-outer">
						<div class="meta-data">
							<div class="date">
								<strong class="date_month"><?php echo date_format($date,'M'); ?></strong>
								<strong class="date_day"><?php echo date_format($date,'d'); ?></strong>
								<strong class="date_year"><?php echo date_format($date,'Y'); ?></strong>
							</div><!-- End .date-->
							<a href="<?php echo $url;?>">
								<h2 class="post-title"><?php echo $post[title]; ?></h2>
							</a>
							<span class="author-vcard" itemprop="author" itemscope="itemscope" itemtype="http://schema.org/Person">
								Post by:<span class="author-name" itemprop="name"><?php echo $post[created_by]; ?></span><br/>
								View:<span class="author-name" itemprop="name"><?php echo $post[hit]; ?></span>
							</span>
						</div><!-- End .meta-data-->
						<div class="post-content">
							<?php echo $post[intro]; ?>
						</div><!-- End .post-content-->
						<div class="jump-link"><a href="<?php echo $url;?>">Read more &raquo;</a></div>
					</div><!-- End .post-outer-->
<?php			
				}
			}
			else {
				echo '<div class="post-outer">There\' no post in this category</div>';
			}
		}
		//Pagination
		private function pagination($cat,$curPage,$rows_per_page)
		{
			$numRows = $this->count_posts($cat[cat_path]);
			if ($numRows == 0) {
				return;
			}
			$numPages = ceil($numRows/$rows_per_page); //Number of pages
			$p = 2; //Show 5 page
			$left = $curPage - $p;
			if ($left <= 0) {
				$left = 1;
				$right = 5;
			}
			else {
				$right = $curPage + 2;
			}
			if ($right > $numPages) {
				$right = $numPages;
				$left = $numPages - 4;
			}
			if ($left <= 0) {
				$left = 1;
			}
			echo '<div class="pagenavi">';
			echo '<span class="pages">Page '.$curPage.' of '.$numPages.'</span>';
			$cat_id = $_GET['id'];
			if ($numPages > 1) {
				if ($curPage > 1) {
					$url = BASEURL.'/index.php?option=com_content&view=category&layout=blog&id='.$cat_id.'&page='.($curPage - 1);
					echo '<a href="'.$url.'">&laquo;</a>';
				}
				for ($i = $left; $i <= $right; $i++) {
					if ($i == $curPage) {
						echo '<span class="current">'.$i.'</span>';
					}
					else {
						$url = BASEURL.'/index.php?option=com_content&view=category&layout=blog&id='.$cat_id.'&page='.$i;
						echo '<a href="'.$url.'">'.$i.'</a>';
					}
				}
				if ($curPage < $numPages) {
					$url = BASEURL.'/index.php?option=com_content&view=category&layout=blog&id='.$cat_id.'&page='.($curPage + 1);
					echo '<a href="'.$url.'">&raquo;</a>';
				}
			}
			echo '</div>';
		}
		//Count posts
		private function count_posts($cat_path)
		{
			$query = "SELECT COUNT(p.id) FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id WHERE p.state=1 AND c.state=1 AND c.cat_path LIKE '".$cat_path."%'";
			$result = mysql_query($query) or die(mysql_error());
			$count = mysql_result($result, 0);
			return intval($count);
		}
		//Update hit
		private function update_post_hit($p_id)
		{
			$query = "UPDATE posts as p SET p.hit=p.hit+1 WHERE p.state=1 AND p.id=$p_id";
			$result = mysql_query($query) or die(mysql_error());
			return mysql_affected_rows();
		}
		//Get featured posts
		private function get_featured_posts()
		{
			$rows = array();
			$query = "SELECT p.id as post_id,p.title,p.intro,p.content,c.id as cat_id,p.created_on FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id WHERE p.state=1 AND c.state=1 AND p.feature=1 ORDER BY p.created_on DESC";
			$result = mysql_query($query) or die(mysql_error());
			$num_row = mysql_num_rows($result);
			if ($num_row > 0) {
				while ($row = mysql_fetch_array($result))
					$rows[] = $row;
				return $rows;
			}
			return null;
		}
		//Get intro text
		private function introContent($str, $limit = 20,$end_char = '&#8230;'){
			if (trim($str) == '') return $str;
			// always strip tags for text
			$str = strip_tags($str);
			preg_match('/\s*(?:\S*\s*){'.(int)$limit.'}/', $str, $matches);		
			if (strlen($matches[0]) == strlen($str))$end_char = '';
			return rtrim($matches[0]).$end_char;
		}
		//Show featured posts
		private function show_featured_posts()
		{

			$posts = $this->get_featured_posts();
?>
			<div id="slider-wrapper">
				<div id="slider" class="nivoSlider">
<?php 		
			foreach ($posts as $post) {
				$imgUrl = $this->get_post_image($post[intro].$post[content]);
				if ($imgUrl == '')
					$imgUrl = BASEURL.'/images/nemo.jpg';
				$timthumbUrl = BASEURL.'/images/thumbnail/timthumb.php';
				$imgUrl = $timthumbUrl.'?src='.$imgUrl.'&amp;w=648&amp;h=258&amp;q=80';
				$postUrl = BASEURL.'/index.php?option=com_content&view=article&id='.$post[post_id];
				$title = "<a class='title' href='".$postUrl."'>".$post[title]."</a>";
				$intro = $this->introContent($post[intro]);
				$title = $title."<p class='intro'>".$intro."</p>";
				echo '<img src="'.$imgUrl.'" alt="featured-images" title="'.$title.'" />';
			}
?>		
				</div>
			</div>
<?php
		}
		//Get image link from post
		private function get_post_image($fulltext) {
			preg_match("/\<img.+?src=\"(.+?)\".+?\/>/", $fulltext, $matches);
			if(count($matches)){
				return $matches[1];
			} else {return '';}
		}
	}
?>